package com.xujl.mvcbind.util;

import com.xujl.fastlib.utils.SpHelper;
import com.xujl.fastlib.utils.StringUtils;
import com.xujl.mvcbind.consts.SpKey;

import java.util.HashMap;
import java.util.Map;

/**
 * @author x
 * @Description
 */
public class RequestCommonParams {

    public static Builder createParamBuilder () {
        return new Builder();
    }

    public static class Builder {
        private final Map<String, Object> map = new HashMap<>();

        /**
         * 默认会添加公共参数
         */
        private Builder () {
            addCommon();
        }

        public Builder clear () {
            map.clear();
            return this;
        }

        public Map<String, String> getHeaderMap () {
            Map<String, String> headers = new HashMap<>();
            String token = SpHelper.getInstance().getString(SpKey.TOKEN);
            if (!StringUtils.isEmpty(token)) {
                headers.put("Auth-Token", token);
            }
            return headers;
        }

        private Builder addCommon () {
            Map<String, String> headers = getHeaderMap();
            if (headers.size() != 0) {
                map.put("headers", headers);
            }
            return this;
        }

        public Builder addParams (String key, Object value) {
            if (value == null || key == null) {
                return this;
            }
            map.put(key, value);
            return this;
        }

        public Map<String, Object> build () {
            return map;
        }

        public Builder remove (String key) {
            if (map.containsKey(key)) {
                map.remove(key);
            }
            return this;
        }
    }
}
